//
//  NetWorkingServices.m
//  YiCuiMiaoZhang
//
//  Created by Sings on 2019/4/1.
//  Copyright © 2018 益萃网络科技（中国）有限公司. All rights reserved.
//

#import "NetWorkingServices.h"

@implementation NetWorkingServices

- (void)requestFunArray:(void (^)(dispatch_group_t group))funArray Over:(void (^)(void))overFlag {
    // 创建队列组
    dispatch_group_t group = dispatch_group_create();
    funArray(group);
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{ overFlag(); });
}

- (void) requestFuntSequentialExecution:(NSMutableArray<NetWorkingServicesAction *> *) marAction Over:(void (^)(BOOL stop))overFlag {
    __block BOOL stop = YES;
    dispatch_queue_t queue = dispatch_queue_create("requestSE", DISPATCH_QUEUE_SERIAL);
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
    for (NetWorkingServicesAction *action in marAction) {
        
        dispatch_async(queue, ^{
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            if (stop) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    NetWorkingServicesActionNextBlock nextBlock = ^(BOOL n_stop){
                        stop = n_stop;
                        dispatch_semaphore_signal(semaphore);
                    };
                    action.action(nextBlock);
                    
                });
            }else {
                dispatch_semaphore_signal(semaphore);
            }
            
        });
    }
    dispatch_async(queue, ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_async(dispatch_get_main_queue(), ^{
            dispatch_semaphore_signal(semaphore);
            overFlag(stop);
        });
    });
}

- (NSString *)netPostVersionTag {
    if (_netPostVersionTag == nil) { _netPostVersionTag = @"0"; }
    int i              = [_netPostVersionTag intValue];
    _netPostVersionTag = [NSString stringWithFormat:@"%d", ++i];
    return _netPostVersionTag;
}

@end

@interface NetWorkingServicesAction ()

@property (nonatomic, readwrite, copy) NetWorkingServicesActionBlock action;

@end

@implementation NetWorkingServicesAction

- (instancetype)initWithAction:(NetWorkingServicesActionBlock)action {
    if (self = [super init]) {
        self.action = action;
    }
    return self;
}

@end
